package com.andyage.admin.service.impl;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Service;
import org.springframework.web.servlet.ModelAndView;

import com.andyage.admin.dao.AdLogMapper;
import com.andyage.admin.dao.AdStaffMapper;
import com.andyage.admin.dao.RoleStaffMapper;
import com.andyage.admin.service.IAdStaffService;
import com.andyage.util.DateUtil;
import com.andyage.util.GetUUID;
import com.andyage.util.JsonContent;
import com.andyage.util.LogUtil;
import com.andyage.util.MD5;
import com.andyage.util.TypeTransfor;
import com.andyage.admin.bean.AdLogBean;
import com.andyage.admin.bean.AdstaffBean;

import net.sf.json.JSONObject;

@Service
public class AdStaffServiceImpl implements IAdStaffService {
	public  String CONTENT_TYPE="text/html;charset=utf-8";
	@Resource
	private AdStaffMapper adstaffMapper;
	@Resource
	private RoleStaffMapper roleStaffMapper;
	@Resource
	private AdLogMapper adlogMapper;
	@Override
	public void AdStaffLogin(HttpServletRequest request, HttpServletResponse response) throws Exception {
		AdstaffBean adstaff = new AdstaffBean();
		HttpSession session=request.getSession();
		String pwd = request.getParameter("staffPassword");
		String account = request.getParameter("staffAccount");
		JSONObject json=new JSONObject();
		if(pwd!=null&&account!=null){
			adstaff.setStaffPassword(MD5.GetMD5Code(pwd));
			adstaff.setStaffAccount(account);
			AdstaffBean adstaffBean = adstaffMapper.AdStaffLogin(adstaff);
			if(adstaffBean!=null){
				AdLogBean adlog = LogUtil.addLog(adstaffBean.getStaffId(), "用户登录", request.getRequestURI(),adstaffBean.getStaffAccount()+"登录");
				adlogMapper.insertLog(adlog);
				json.accumulate("result", 1);
				json.accumulate("tips", "登录成功");
				session.setAttribute("AccountId",adstaffBean.getStaffId() );
				session.setAttribute("StaffAccount", adstaffBean.getStaffAccount());
				session.setMaxInactiveInterval(60*60);
			}else{
				json.accumulate("result", 0);
				json.accumulate("tips", "登录失败");
			}
		}else{
			json.accumulate("result", 2);
			json.accumulate("tips", "用户名或密码为空");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);

	}

	@Override
	public void queryAdStaffInfo(AdstaffBean adstaff, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
			List<AdstaffBean> Adstaff = adstaffMapper.queryAdStaffInfo(adstaff);
			int total = adstaffMapper.queryAdStaffInfoCount(adstaff);
			String json = JsonContent.getJsonArray(Adstaff, total);
			response.setContentType(CONTENT_TYPE);
			response.getWriter().print(json);
	}

	@Override
	public void addAdStaffInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		AdstaffBean adstaff = new AdstaffBean();
		String staffIsOn=request.getParameter("staffIsOn");
		if(staffIsOn!=null&&!"".equals(staffIsOn)){
		if (TypeTransfor.isNumber(staffIsOn)) {
			adstaff.setStaffIsOn(Integer.valueOf(staffIsOn));
		} else {
			adstaff.setStaffIsOn(Integer.valueOf(TypeTransfor.yesOrNoTransfor(staffIsOn)));
		}
		}
		adstaff.setStaffId(GetUUID.get());
		adstaff.setStaffPassword(MD5.GetMD5Code(request.getParameter("staffPass")));
		adstaff.setStaffLeaveTime(DateUtil.getDate(request.getParameter("staffLeaveTime")));
		adstaff.setStaffEntryTime(DateUtil.getDate(request.getParameter("staffEntryTime")));
		adstaff.setStaffAccount(request.getParameter("staffAccount"));
		adstaff.setStaffName(request.getParameter("staffName"));
		adstaff.setStaffTelephone(Long.valueOf(request.getParameter("staffTelephone")));
		int flag=adstaffMapper.addAdStaffInfo(adstaff);
		JSONObject json=new JSONObject();
		if(flag!=0){
			json.accumulate("result", 1);
			json.accumulate("tips", "新增成功");
		}else{
			json.accumulate("result", 0);
			json.accumulate("tips", "新增失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);

	}

	@Override
	public void deleteAdStaffInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String staffId = request.getParameter("staffId");
		roleStaffMapper.deleteRoleStaff(staffId);//删除用户执行删除用户身份关联表的数据
		int flag=adstaffMapper.deleteAdStaffInfo(staffId);
		System.out.println(flag);
		JSONObject json=new JSONObject();
		if(flag!=0){
			json.accumulate("result", 1);
			json.accumulate("tips", "删除成功");
		}else{
			json.accumulate("result", 0);
			json.accumulate("tips", "删除失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void updateAdStaffInfo(HttpServletRequest request, HttpServletResponse response) throws Exception {
		AdstaffBean adstaff = new AdstaffBean();
		adstaff.setStaffId(request.getParameter("staffId"));
		String staffPass=request.getParameter("staffPass");
		if(!"***".equals(staffPass)){
			adstaff.setStaffPassword(MD5.GetMD5Code(staffPass));
		}
		adstaff.setStaffLeaveTime(DateUtil.getDate(request.getParameter("staffLeaveTime")));
		adstaff.setStaffEntryTime(DateUtil.getDate(request.getParameter("staffEntryTime")));
		adstaff.setStaffAccount(request.getParameter("staffAccount"));
		adstaff.setStaffName(request.getParameter("staffName"));
		adstaff.setStaffTelephone(Long.valueOf(request.getParameter("staffTelephone")));
		String staffIsOn = request.getParameter("staffIsOn");
		System.out.println(staffIsOn);
		if(staffIsOn!=null&&!"".equals(staffIsOn)){
		if (TypeTransfor.isNumber(staffIsOn)) {
			adstaff.setStaffIsOn(Integer.valueOf(staffIsOn));
		} else {
			adstaff.setStaffIsOn(Integer.valueOf(TypeTransfor.yesOrNoTransfor(staffIsOn)));
		}
		}
		int flag=adstaffMapper.updateAdStaffInfo(adstaff);
		System.out.println(flag);
		JSONObject json=new JSONObject();
		if(flag!=0){
			json.accumulate("result", 1);
			json.accumulate("tips", "修改成功");
		}else{
			json.accumulate("result", 0);
			json.accumulate("tips", "修改失败");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void queryCommonStaff(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String staffAccount = request.getParameter("staffAccount");
		AdstaffBean adstaff = adstaffMapper.queryCommonStaff(staffAccount);
		JSONObject json=new JSONObject();
		if(adstaff!=null){
			json.accumulate("result", 0);
			json.accumulate("tips", "用户已存在");
		}else{
			json.accumulate("result", 1);
			json.accumulate("tips", "");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

	@Override
	public void updateStaffPwd(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String staffId = request.getParameter("AccountId");//用户的id
		String password = request.getParameter("password");//用户的密码
		String newPass = request.getParameter("newPass");
		String renewPass = request.getParameter("renewPass");
		AdstaffBean adstaff = new AdstaffBean();
		adstaff.setStaffId(staffId);
		adstaff.setStaffPassword(MD5.GetMD5Code(password));
		AdstaffBean bean =	adstaffMapper.AdStaffLogin(adstaff);
		JSONObject json = new JSONObject();
		if(bean!=null){
			if(newPass.equals(renewPass)&&newPass!=null&&!"".equals(newPass)){
				adstaff.setStaffPassword(MD5.GetMD5Code(newPass));
				int flag = adstaffMapper.updateStaffPwd(adstaff);
				if(flag!=0){
					json.accumulate("result", 1);
					json.accumulate("tips", "修改密码成功");
				}else{
					json.accumulate("result", 0);
					json.accumulate("tips", "密码修改失败");
				}
			}else{
				json.accumulate("result", 3);
				json.accumulate("tips", "两次密码不同或为空");
			}
		}else{
			json.accumulate("result", 2);
			json.accumulate("tips", "密码输入错误,请重新输入");
		}
		response.setContentType(CONTENT_TYPE);
		response.getWriter().print(json);
	}

}
